/**
 * Created by zhaoyanzhi on 16-9-23.
 */
app.controller('productListPage', function ($scope, $location, $http,$q) {
        $http.get("../json/productListPage.json").success(
            function (response) {
                $scope.productList = response;
            }
        );
        $scope.showUserName = function () {
            var data=JSON.parse(localStorage.getItem("AV/RaXOd37qSYs8fKCN82nbC9zd-gzGzoHsz/currentUser"));
            $scope.userName = data.username;
        };
        $scope.showUserName();
        $scope.getUserId = function () {
            var userId = JSON.parse(localStorage.getItem("AV/RaXOd37qSYs8fKCN82nbC9zd-gzGzoHsz/currentUser")).objectId;
            return userId
        };
    $scope.showCartCount = function(){
        var defer = $q.defer();
        var promise = defer.promise;
        var userId = $scope.getUserId();
        var user=AV.Object.createWithoutData('User', userId);
        var relation=user.relation('productInfo');
        var query=relation.query();
        promise.then( function(sumCount){
            $scope.cartCount=sumCount;
        })
        defer.resolve( query.find().then(function(results){
                var sumCount=ShoppingCart.computeSumCount(results);
                return sumCount
            }
        ));
    };
    $scope.showCartCount();
    $scope.addButtonEvent = function(product,num){
        var userId=$scope.getUserId();
        var user=AV.Object.createWithoutData('User', userId);
        var relation=user.relation('productInfo');
        var query = relation.query();
        query.find().then(function(results){
            var itemName = [];
            for (var i = 0; i < results.length; i++) {
                itemName.push(results[i].get('itemName'))
            }
            var index=itemName.indexOf(product.itemName);
            if(index==-1){
                ShoppingCart.addProduct(product, user);
            }else{
                var goodsInfo=AV.Object.createWithoutData('GoodsInfo',results[index].id);
                var  itemInfo=Item.computeItemCount(results[index],num);
                var  itemInfo=Item.computeCostPrice(itemInfo);
                var  promotion=new Promotion();
                var  itemInfo=promotion.computePromotionCount(itemInfo);
                var  itemInfo=promotion.computeSavePrice(itemInfo);
                var  itemInfo=ShoppingCart.computeActuallyPaid(itemInfo);
                goodsInfo.set('count',itemInfo.attributes.count);
                goodsInfo.set('costPrice',JSON.stringify(itemInfo.attributes.costPrice));
                goodsInfo.set('promotionCount',itemInfo.attributes.promotionCount);
                goodsInfo.set('savePrice',itemInfo.attributes.savePrice);
                goodsInfo.set('actuallyPaid',JSON.stringify(itemInfo.attributes.actuallyPaid));
                goodsInfo.save();
                $scope.showCartCount()
            }
        }, function (error) {
        });
    }
    $scope.jumpShoppingCartPage=function(){
        $location.path("/shoppingCartPage")
    }
});




